package com.ruoyi.project.price.service.impl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import cn.hutool.core.lang.Snowflake;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.project.income.domain.BinsMerchantAccount;
import com.ruoyi.project.income.service.IBinsMerchantAccountService;
import com.ruoyi.project.price.domain.BinsAccount;
import com.ruoyi.project.price.service.IBinsAccountService;
import com.ruoyi.project.user.domain.UserInfo;
import com.ruoyi.project.user.service.IUserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.price.mapper.BinsAccountTraceMapper;
import com.ruoyi.project.price.domain.BinsAccountTrace;
import com.ruoyi.project.price.service.IBinsAccountTraceService;

/**
 * 账户流水Service业务层处理
 * 
 * @author administrator
 * @date 2025-04-03
 */
@Service
public class BinsAccountTraceServiceImpl implements IBinsAccountTraceService 
{
    @Autowired
    private BinsAccountTraceMapper binsAccountTraceMapper;
    @Autowired
    Snowflake snowflake;
    @Autowired
    private IBinsAccountTraceService binsAccountTraceService;
    @Autowired
    private IBinsAccountService binsAccountService;
    @Autowired
    private IUserInfoService userInfoService;

    /**
     * 查询账户流水
     * 
     * @param traceSn 账户流水主键
     * @return 账户流水
     */
    @Override
    public BinsAccountTrace selectBinsAccountTraceByTraceSn(String traceSn)
    {
        return binsAccountTraceMapper.selectBinsAccountTraceByTraceSn(traceSn);
    }

    /**
     * 查询账户流水列表
     * 
     * @param binsAccountTrace 账户流水
     * @return 账户流水
     */
    @Override
    public List<BinsAccountTrace> selectBinsAccountTraceList(BinsAccountTrace binsAccountTrace)
    {
        return binsAccountTraceMapper.selectBinsAccountTraceList(binsAccountTrace);
    }
    @Override
    public BinsAccountTrace selectBinsAccountTraceSUM(BinsAccountTrace binsAccountTrace){
        return binsAccountTraceMapper.selectBinsAccountTraceSUM(binsAccountTrace);
    }
    /**
     * 新增账户流水
     * 
     * @param binsAccountTrace 账户流水
     * @return 结果
     */
    @Override
    public int insertBinsAccountTrace(BinsAccountTrace binsAccountTrace)
    {
        binsAccountTrace.setTraceSn(String.valueOf(snowflake.nextId()));
        return binsAccountTraceMapper.insertBinsAccountTrace(binsAccountTrace);
    }

    /**
     * 修改账户流水
     * 
     * @param binsAccountTrace 账户流水
     * @return 结果
     */
    @Override
    public int updateBinsAccountTrace(BinsAccountTrace binsAccountTrace)
    {
        binsAccountTrace.setUpdateTime(DateUtils.getNowDate());
        return binsAccountTraceMapper.updateBinsAccountTrace(binsAccountTrace);
    }

    /**
     * 批量删除账户流水
     * 
     * @param traceSns 需要删除的账户流水主键
     * @return 结果
     */
    @Override
    public int deleteBinsAccountTraceByTraceSns(String[] traceSns)
    {
        return binsAccountTraceMapper.deleteBinsAccountTraceByTraceSns(traceSns);
    }

    /**
     * 删除账户流水信息
     * 
     * @param traceSn 账户流水主键
     * @return 结果
     */
    @Override
    public int deleteBinsAccountTraceByTraceSn(String traceSn)
    {
        return binsAccountTraceMapper.deleteBinsAccountTraceByTraceSn(traceSn);
    }

    @Override
    public BinsAccountTrace getAccountTrace2Pay(String userId, BigDecimal orderPrice, String paySource) {
        UserInfo userInfo = userInfoService.selectUserInfoByUserId(userId);

        BinsAccount binsAccount = binsAccountService.selectBinsAccountByUserId(userId);

        if (((binsAccount.getTotalAmount()).add(orderPrice)).compareTo(BigDecimal.ZERO) < 0) {
            throw new RuntimeException("余额不足");
        }
        BinsAccountTrace binsAccountTrace = new BinsAccountTrace();
        binsAccountTrace.setUserId(userId);
        binsAccountTrace.setAccountNum(binsAccount.getAccountNum());
        binsAccountTrace.setAmount(orderPrice);
        binsAccountTrace.setTotalAmount(binsAccount.getTotalAmount());
        binsAccountTrace.setAddTime(new Date());
        binsAccountTrace.setMobile(userInfo.getDriverContact());
        binsAccountTrace.setStatus(0);
        binsAccountTrace.setCreateBy("system");
        binsAccountTrace.setCreateTime(new Date());
        binsAccountTrace.setType(paySource);
        int count = binsAccountTraceService.insertBinsAccountTrace(binsAccountTrace);
        if (count == 0) {
            throw new RuntimeException("支付失败");
        }
        // 更新账户余额
        binsAccount.setTotalAmount(binsAccount.getTotalAmount().add(orderPrice));
        binsAccount.setUpdateTime(new Date());
        binsAccountService.updateBinsAccount(binsAccount);

        return binsAccountTrace;
    }
}
